//
//  DefaultSortView.m
//  New_Runway
//
//  Created by mack on 2018/2/2.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "DefaultSortView.h"

@interface DefaultSortView ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation DefaultSortView
{
    NSIndexPath *_selIndex;
}
-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];
        [self initTableview];
    }
    return self;
}
-(void)updateSelected{
    _selIndex = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableview reloadData];
}
-(void)initTableview{
//    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
//        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self addSubview:self.tableview];

    self.tableview.frame = CGRectMake(0, 0, Screen_width, 0);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 6;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str;
    NSString *str1;
    if ([self.type isEqualToString:kBuyCar]) {
        str = @"最新上架（默认）";
        str1 = @"最早上架";
    }else if ([self.type isEqualToString:kCarInLibary]){
        str = @"最新入库（默认）";
        str1 = @"最早入库";
    }else if ([self.type isEqualToString:kCarOutLibary]){
        str = @"最新出库（默认）";
        str1 = @"最早出库";
    }else if ([self.type isEqualToString:kCarOrder]){
        str = @"最新预定（默认）";
        str1 = @"最早预定";
    }else if ([self.type isEqualToString:kCarNoLibary]){
        str = @"最新更新（默认）";
        str1 = @"最早更新";
    }else if ([self.type isEqualToString:kCarInLibaryApplying] || [self.type isEqualToString:kCarOutLibaryApplying]){
        str = @"最新申请（默认）";
        str1 = @"最早申请";
    }
    NSArray *array = @[str,str1,@"价格最高",@"价格最低",@"车龄最短",@"里程最短"];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = UIColorHex(0xf5f5f5);
    line.frame = CGRectMake(0, 0, Screen_width, 0.5);
    [cell.contentView addSubview:line];
    cell.textLabel.text = array[indexPath.row];
    cell.tintColor = MAIN_NAV_COLOR();
    cell.textLabel.font = kFontSize(12);
    if (_selIndex == indexPath) {
        cell.textLabel.textColor = MAIN_NAV_COLOR();
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.textColor = MAIN_FONT_COLOR();
        
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *celled = [tableView cellForRowAtIndexPath:_selIndex];
    celled.accessoryType = UITableViewCellAccessoryNone;
    celled.textLabel.textColor = MAIN_FONT_COLOR();
    //记录当前选中的位置索引
    _selIndex = indexPath;
    //当前选择的打勾
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.textLabel.textColor = MAIN_NAV_COLOR();
    self.selectedBlock(indexPath.row);
}
@end
